package com.jiudengnile.liushu.biz.ao.impl;

import wint.help.biz.result.Result;
import wint.help.biz.result.ResultSupport;

import com.jiudengnile.liushu.biz.ao.AdminAO;
import com.jiudengnile.liushu.biz.bo.PermissionBO;
import com.jiudengnile.liushu.biz.constants.PermissionCodes;
import com.jiudengnile.liushu.biz.domains.permission.PermissionDO;
import com.jiudengnile.liushu.biz.query.ListWithTotalCount;
import com.jiudengnile.liushu.biz.query.PermissionQuery;
import com.jiudengnile.liushu.biz.resultcodes.PermissionResultCodes;
import com.jiudengnile.liushu.common.request.RequestUser;

public class AdminAOImpl extends BaseAO implements AdminAO {

	private PermissionBO permissionBO;
	
	private int operatePermissionPageSize = 50;
	
	@Override
	public Result viewAdminPage(int page) {
		Result result = new ResultSupport(false);
		try {
			RequestUser requestUser = getRequestUser();
			if (!permissionBO.hasPermission(requestUser.getUserId(), PermissionCodes.VIEW_ADMIN_PAGE)) {
				result.setResultCode(PermissionResultCodes.NO_PERMISSION);
				return result;
			}
			
			PermissionQuery operatePermissionQuery = new PermissionQuery();
			operatePermissionQuery.setPageNo(page);
			operatePermissionQuery.setPageSize(operatePermissionPageSize);
			operatePermissionQuery.setUserId(requestUser.getUserId());
			
			ListWithTotalCount<PermissionDO> operatePermissions = permissionBO.getPermissionByUser(operatePermissionQuery);
			operatePermissionQuery.setTotalResultCount(operatePermissions.getTotalCount());
			
			result.getModels().put("operatePermissions", operatePermissions);			
			result.setSuccess(true);
		} catch (Exception e) {
			log.error("viewAdminPage error", e);
		}
		return result;
	}

	public void setPermissionBO(PermissionBO permissionBO) {
		this.permissionBO = permissionBO;
	}

}
